package cs2340.flyingkiwi.todoapp;

import java.util.List;

/**
 * Manages all the accounts with the ability to create an account and login.
 * @author Group Flying Kiwi
 * @version 3 Mar 2012
 */
public class ProgramManager
{
	private ProgramInfo pInfo;
	
	public ProgramManager(){
		pInfo = new ProgramInfo();
	}
	
	/**
	 * Takes in and checks info to create an account and makes a new AccountInfo
	 * if info is correct.
	 * @param username String that holds username
	 * @param password1 String that holds password typed the first time
	 * @param password2 String that holds password typed the second time
	 * @param name String that holds name
	 * @param email String that holds email
	 * @return boolean if a new AccountInfo was created
	 */
	public boolean createAccount(String username, String password1, String password2,
			String name, String email)
	{
		if(password1.equals(password2) && (username.length() != 0) 
				&& (password1.length() != 0) && (name.length() != 0) 
				&& (email.length() != 0))
		{
			AccountInfo account = new AccountInfo(username, password1, password2, 
				name, email);
			pInfo.addAccountInfo(account);
			return true;
		}
		return false;
	}
		
	/**
	 * Takes in username and password. Uses AccountInfo to check if username 
	 * matches an account and if password matches the username.
	 * @param username
	 * @param password
	 * @return boolean if username and password match
	 */
	public boolean login(String username, String password)
	{
		List<AccountInfo> accounts = pInfo.getAccounts();
		for(int i = 0; i < accounts.size(); i++)
		{
			AccountInfo account = accounts.get(i);
			if(account.getUsername().equals(username))
				if(account.getPassword().equals(password))
					return true;
		}
		return false;
	}
}